// CS 564:: PROJECT STAGE 5 - Catalog
//
// Caela Northey (caela) 905 653 2238
// Colin Laska   (laska) 906 150 7258

#include <sys/types.h>
#include <functional>
#include <string.h>
#include <stdio.h>
using namespace std;

#include "error.h"
#include "utility.h"
#include "catalog.h"

// define if debug output wanted

//
// Retrieves and prints information from the catalogs about the for the
// user. If no relation is given (relation is NULL), then it lists all
// the relations in the database, along with the width in bytes of the
// relation, the number of attributes in the relation, and the number of
// attributes that are indexed.  If a relation is given, then it lists
// all of the attributes of the relation, as well as its type, length,
// and offset, whether it's indexed or not, and its index number.
//
// Returns:
// 	OK on success
// 	error code otherwise
//

const Status RelCatalog::help(const string & relation)
{
    Status status;
    RelDesc rd;
    AttrDesc *attrs;
    int attrCnt;

    if (relation.empty()) return UT_Print(RELCATNAME);

    //check if relation exists
    if (relCat->getInfo(relation, rd) == RELNOTFOUND)
      {
	return RELNOTFOUND;
      }

    // Get attribute data
    if ((status = attrCat->getRelInfo(relation, attrCnt, attrs)) != OK)
        return status;

    cout << "Relation name: " << relation << 
"\n\nattrName\ttype\toffset\tlen\n----------------------------------------\n";

    std::string type;

    // Loop through the array
    for(int i = 0; i < attrCnt; i++)
    {
        switch(attrs[i].attrType)
        {
        case INTEGER:
            type = "INTEGER";
            break;
        case FLOAT:
            type = "FLOAT";
            break;
        case STRING:
            type = "STRING";
            break;
        default:
            type = "UNKNOWN TYPE";
            break;
        }

        /* RECALL:
        typedef struct {
        char relName[MAXNAME];          // relation name            
        char attrName[MAXNAME];         // attribute name           
        int attrOffset;                 // attribute offset         
        int attrType;                   // attribute type           
        int attrLen;                    // attribute length         
        } AttrDesc; */

        std::cout << attrs[i].attrName << "\t\t" << type << "\t" << attrs[i].attrOffset << "\t" << attrs[i].attrLen << "\n";
    }

    delete attrs;

    return OK;
}
